<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="会议主题" prop="subject">
				<el-input v-model="form.subject" placeholder="会议主题" clearable maxlength="180"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="会议类型" prop="meetingType">
						<sc-select v-model="form.meetingType" :params="{code:'meetingType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="联系电话" prop="tel">
						<el-input v-model="form.tel" placeholder="联系电话" clearable maxlength="40"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="会议主持" prop="chair">
						<select-user-dialog v-model:accountId="form.chair" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="会议纪要员" prop="notesUser">
						<select-user-dialog v-model:accountId="form.notesUser" :mode="false"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开始时间" prop="beginTime">
						<el-date-picker v-model="form.beginTime" :disabled-date="publishDateAfter" placeholder="开始时间" type="datetime" value-format="YYYY-MM-DD HH:mm" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker v-model="form.endTime" :disabled-date="publishDateAfter" placeholder="结束时间" type="datetime" value-format="YYYY-MM-DD HH:mm" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="会议室" prop="roomId">
						<el-select v-model="form.roomId" clearable filterable style="width: 100%">
							<el-option v-for="item in roomList" :value="item.roomId" :label="item.name" :key="item.roomId"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="会议设备" prop="deviceId">
						<el-select v-model="form.deviceId" multiple placeholder="请选择" style="width: 100%">
							<el-option
								v-for="item in deviceList"
								:key="item.deviceId"
								:label="item.deviceName"
								:value="item.deviceId">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="参会人员" prop="userJoin">
				<select-user-dialog v-model:accountId="form.userJoin" :mode="true"/>
			</el-form-item>
			<el-form-item label="参会部门" prop="deptJoin">
				<select-dept-dialog v-model:deptId="form.deptJoin" :mode="true"/>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="24">
					<el-form-item label="参会职务" prop="levelJoin">
						<select-user-level-dialog v-model:levelId="form.levelJoin" :mode="true"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="外部人员" prop="otherJoin">
				<el-input type="textarea" v-model="form.otherJoin" placeholder="外部人员" clearable></el-input>
			</el-form-item>
			<el-form-item label="会议内容">
				<sc-editor v-model="form.content" placeholder="请输入" :templates="templates" :height="200" clearable style="width: 100%;"></sc-editor>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="是否加入日程" prop="joinCal">
						<el-radio-group v-model="form.joinCal">
							<el-radio-button label="0">不加入</el-radio-button>
							<el-radio-button label="1">加入</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="附件权限" prop="attachRole">
						<el-radio-group v-model="form.attachRole">
							<el-radio-button label="1">查看</el-radio-button>
							<el-radio-button label="2">下载</el-radio-button>
							<el-radio-button label="3">打印</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="会议资料" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'meeting'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="meeting"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import {defineAsyncComponent} from "vue";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from '@/views/module/attachMenu'
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserLevelDialog,
		msgBoxCheckBox,
		selectUserDialog,
		selectDeptDialog,
		AttachMenu,
		scEditor
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			roomList:[],
			deviceList:[],
			mode: "add",
			titleMap: {
				add: '会议预约对话框',
				edit: '会议编辑对话框',
				show: '查看'
			},
			fileList: [],
			apiObj: this.$API.file.attach.uploadFile,
			templates: [],
			form: {
				meetingId: '',
				subject: '',
				meetingType: '',
				tel: '',
				chair: '',
				notesUser: '',
				roomId: '',
				deviceId: [],
				beginTime: '',
				endTime: '',
				joinCal: '0',
				attachRole: '1',
				userJoin: '',
				deptJoin: '',
				levelJoin: '',
				otherJoin: '',
				content: '',
				msgType: '',
				attachId: ''
			},
			rules: {
				subject: [
					{required: true, message: '会议主题不能为空', trigger: 'blur'}
				],
				meetingType: [
					{required: true, message: '会议类型不能为空', trigger: 'blur'}
				],
				chair: [
					{required: true, message: '会议主持不能为空', trigger: 'blur'}
				],
				notesUser: [
					{required: true, message: '会议经要员不能为空', trigger: 'blur'}
				],
				beginTime: [
					{required: true, message: '会议开始时间不能为空', trigger: 'blur'}
				],
				endTime: [
					{required: true, message: '会议结束时间不能为空', trigger: 'blur'}
				],
				roomId: [
					{required: true, message: '请选择会议室', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getCanUseDeviceList();
	},
	methods: {
		publishDateAfter(time){
			return time.getTime()<(Date.now()-24*60*60*1000)
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.form.deviceId = this.form.deviceId.join(",");
					var res;
					if (this.mode == "add") {
						res = await this.$API.office.meeting.insertMeeting.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.office.meeting.updateMeeting.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		async getCanUseDeviceList()
		{
			var res = await this.$API.office.meeting.getCanUseDeviceList.get();
			if (res.code == 200) {
				this.deviceList = res.data;
			}
		},
		setCreateData(data,roomList)
		{
			this.roomList = roomList;
			this.form.roomId = data.roomId;
			this.form.beginTime = data.beginTime;
			this.form.endTime = data.endTime;
		},
		setData(data,roomList) {
			this.roomList = roomList;
			this.form.meetingId = data.meetingId
			this.form.subject = data.subject
			this.form.meetingType = data.meetingType
			this.form.tel = data.tel
			this.form.chair = data.chair
			this.form.notesUser = data.notesUser
			this.form.roomId = data.roomId
			if(data.deviceId!='')
			{
				this.form.deviceId = data.deviceId.split(",");
			}else {
				this.form.deviceId =[];
			}
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.joinCal = data.joinCal
			this.form.attachRole = data.attachRole
			this.form.userJoin = data.userJoin
			this.form.deptJoin = data.deptJoin
			this.form.levelJoin = data.levelJoin
			this.form.otherJoin = data.otherJoin
			this.form.content = data.content
			this.form.msgType = data.msgType
			this.form.attachId = data.attachId
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
